package com.lanqiao.attendance.controller;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.lanqiao.attendance.pojo.Attendance;
import com.lanqiao.attendance.service.AttendanceService;
import com.lanqiao.attendance.utils.QRCodeUtil;
import com.lanqiao.attendance.vo.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@RestController
@RequestMapping("attendance")
@CrossOrigin
@Api(tags = "签到信息接口")
public class AttendanceController {
    @Autowired
    AttendanceService attendanceService;

    @GetMapping("queryAll")
    @ApiOperation("查询所有签到信息")
    public Result queryAll() {
        return attendanceService.queryAll();
    }

    @GetMapping("list")
    @ApiOperation("根据课程号，查询课程的签到信息")
    public Result queryByCourseNum(Long courseId) {
        return attendanceService.queryByCourseNum(courseId);
    }

    @PostMapping("insert")
    @ApiOperation("新增课程签到信息")
    public Result insert(@RequestBody Attendance attendance) {
        System.out.println(attendance.getCourseId());
        return attendanceService.insert(attendance);
    }

    @GetMapping("update")
    @ApiOperation("根据学生id和课程id，更新status=1")
    public Result update(@RequestParam("stuId") Long stuId, @RequestParam("courseId") Long courseId) {
        return attendanceService.update(stuId, courseId);
    }

    @DeleteMapping("delete")
    @ApiOperation("根据学生id和课程号删除签到记录")
    public Result delete(Long stuId, Long courseId) {
        return attendanceService.delete(stuId, courseId);
    }

    @GetMapping("attendanceCount")
    @ApiOperation("统计签到人数，status=0：未签到人数，status=1：已签到人数")
    public Result attendanceCount(Long courseId, String status) {
        return attendanceService.attendanceCount(courseId, status);
    }

    /**
     * 生成微信图片二维码
     *
     * @param request
     * @param response
     * @param content   为前端传过来的二维码的内容，即路径链接
     * @throws Exception
     */
    @GetMapping("/qrcode")
    public Result qrcode(HttpServletRequest request, HttpServletResponse response, @RequestParam(name = "content") String content) throws Exception {
        if (StringUtils.isBlank(content)) {
            response.sendRedirect("/404.html");
            return Result.fail(3003,"生成二维码失败");
        }
        //调用工具类，生成二维码
        QRCodeUtil.creatRrCode(content, 180,180,response);   //180为图片高度和宽度
        return Result.success("创建二维码成功",null);
    }

    /**
     * 查询某次排课的学生签到信息列表
     * @param id
     * @return
     */
    @GetMapping("/student/{id}")
    public Result queryStudentAttendance(@PathVariable("id") Long id) throws IOException {
        return attendanceService.queryStudentAttendance(id);
    }
}
